﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Model;
using Kiiro.SiteDefinition.ControlTemplates;
using Kiiro.SiteDefinition.Core;

namespace Kiiro.SiteDefinition.ProjectPages
{
    public partial class EditComment : BasePage
    {
        private Comment comment = new Comment ( );
        public Comment Comment
        {
            get { return comment; }
            set { comment = value; }
        }

        public NavigationSub NavigationSub
        {
            get { return KiiroMaster.NavigationSub; }
        }

        public AttachmentControl Attachments
        {
            get { return ( AttachmentControl ) attachmentControl; }
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
            litTitle.Text = "Edit Comment";
            if ( KiiroMaster.V2Header.Project != null && !string.IsNullOrEmpty ( KiiroMaster.V2Header.Project.Title ) )
            {
                litTitle.Text = KiiroMaster.V2Header.Project.Title + " - Edit Comment";
            }

            if ( ItemId > 0 )
            {
                Comment = CommentModel.GetById ( ItemId, SiteCollectionId, ServerRelativeUrl );

                if ( Comment != null )
                {
                    // override page permissions and get object level permissions
                    PermissionsModel.GetGlobalPermissionBitsForCurrentUser ( Comment, CurrentUser, SiteCollectionId, ServerRelativeUrl );

                    if ( Comment.PermissionBitsDiscussions != null && Comment.PermissionBitsDiscussions.Count > 0 &&
                        Comment.PermissionBitsDiscussions [ 2 ] )
                    {
                        if ( !Page.IsPostBack )
                        {
                            PopulateComment ( Comment );
                        }
                    }
                    else // security error
                        Redirect ( string.Empty );
                }
                else
                    Redirect ( string.Empty );
            }
            else
                Redirect ( string.Empty );
        }

        private void PopulateComment ( Comment comment )
        {
            txtCommentText.Text = comment.Text;
            Attachments.SetAttachments ( comment.AttachmentList );
        }

        protected void btnCancel_Click ( object sender, EventArgs e )
        {
            Redirect ( string.Empty );
        }

        protected void btnSave_Click ( object sender, EventArgs e )
        {
            if ( Comment.PermissionBitsDiscussions [ 2 ] )
            {
                Comment.EditType = EditType.Updated;
                Comment.ModifiedBy = CurrentUser;
                Comment.ModifiedDate = DateTime.Now;

                Comment.AttachmentList = Attachments.GetAttachments ( );

                Comment.Text = txtCommentText.Text;

                Comment = CommentModel.Save ( Comment, SiteCollectionId, ServerRelativeUrl );

                Redirect ( Comment );
            }
        }

        private void Redirect ( string redirectUrl )
        {
            string url = default ( string );

            url = ServerRelativeUrl + Constants.ProjectFileUrls.DiscussionsAll;

            if ( !string.IsNullOrEmpty ( redirectUrl ) )
            {
                url = redirectUrl;
            }

            if ( Request.Params [ "Source" ] != null )
            {
                url = Request.Params [ "Source" ];
            }

            Response.Redirect ( url );
        }

        private void Redirect ( Comment comment )
        {
            if ( comment.IsValid ( ) )
            {
                Response.Redirect ( String.Format ( "{0}{1}?id={2}", ServerRelativeUrl, Constants.ProjectFileUrls.DiscussionDisplay, comment.DiscussionId ) );
            }
            else
            {
                lblCommentTextMessage.Text = FormattingHelpers.GetErrorMessageText ( comment.TextMessage );
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/FEATURES/Kiiro.ProjectPages/Discussions/EditComment.aspx.cs $
 * 
 * 9     2/08/10 2:28p Camerons
 * 
 * 8     1/06/10 12:39p Camerons
 * 
 * 7     26/11/09 2:23p Shafia
 * 
 * 6     30/09/09 4:08p Camerons
 * Update all links that point to UserPages, pass User.SPUserId as the
 * ItemId so that we can safely run GetOrCreateUser
 * 
 * 5     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/